package com.sam.designpattern.builder;

import java.util.Collection;
import java.util.Map;

/**
 * @Author: Sam
 * @Date: 2022-07-25 09:43
 * @Description: 实现导出数据到Xml的生成器对象
 */
public class XmlBuilder implements Builder {
    /**
     * 生成器最终构建的产品对象，相当于产品
     */
    private StringBuffer buffer = new StringBuffer();

    public StringBuffer getResult() {
        return buffer;
    }

    @Override
    public void buildHeader(ExportHeaderModel ehm) {
        buffer.append("<?xml version='1.0' encoding='gb2312'?>\n");
        buffer.append("<Report>\n");
        buffer.append("  <Header>\n");
        buffer.append("    <DeptId>" + ehm.getDeptId() + "</DeptId>\n");
        buffer.append("    <ExportDate>" + ehm.getExportDate() + "</ExportDate>\n");
        buffer.append("  </Header>\n");
    }

    @Override
    public void buildBody(Map<String, Collection<ExportDataModel>> mapData) {
        buffer.append("  <Body>\n");
        for (String tblName : mapData.keySet()) {
            buffer.append("    <Datas TableName =\"" + tblName + "\">\n");
            for (ExportDataModel edm : mapData.get(tblName)) {
                buffer.append("      <Data>\n");
                buffer.append("        <ProductId>" + edm.getProductId() + "</ProductId>\n");
                buffer.append("        <Price>" + edm.getPrice() + "</Price>\n");
                buffer.append("        <Amount>" + edm.getAmount() + "</Amount>\n");
                buffer.append("      </Data>\n");
            }
            buffer.append("    </Datas>\n");
        }
        buffer.append("  </Body>\n");
    }

    @Override
    public void buildFooter(ExportFooterModel efm) {
        buffer.append("  <Footer>\n");
        buffer.append("    <ExportUser>" + efm.getExportUser() + "</ExportUser>\n");
        buffer.append("  </Footer>\n");
        buffer.append("</Report>\n");


    }
}
